package com.tuanzi.blog.service.impl;

import com.tuanzi.blog.dao.BlogMapper;
import com.tuanzi.blog.dao.CommentMapper;
import com.tuanzi.blog.model.Comment;
import com.tuanzi.blog.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private BlogMapper blogMapper;

    @Value("${comment.avatar}")
    private String avatar;

    /**
     * 查询所有的评论内容
     */
    @Transactional
    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        //拿到所有的顶级评论
        List<Comment> comments = commentMapper.selectTopByBlogId(blogId);
        for (Comment comment : comments) {
            comment.setBlog(blogMapper.selectByPrimaryKey(blogId));
            //这里为它的孩子赋值
            comment.setCommentList(commentMapper.selectChildrens(comment.getId()));
        }
        List<Comment> commentList = eachComment(comments);
        return commentList;
    }

    /**
     * 保存前端发送的评论
     */
    @Transactional
    @Override
    public Comment saveComment(Comment comment) {
        Long parentCommentId = comment.getParentComment().getId();
        if (parentCommentId != -1){
            comment.setParentComment(commentMapper.selectByPrimaryKey(parentCommentId));
        }else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        comment.setParentCommentId(parentCommentId);
        comment.setBlogId(comment.getBlog().getId());
        comment.setAvatar(avatar);
        int insert = commentMapper.insert(comment);
        if (insert == 1){
            return comment;
        }
        return null;
    }

    //循环每个顶级的评论节点
    public List<Comment> eachComment(List<Comment> comments){
        //合并评论的各层子集到第一级子代集合中
        combineChildren(comments);
        return comments;
    }

    private void combineChildren(List<Comment> comments){

        for (Comment comment : comments) {
            List<Comment> replays = comment.getCommentList();
            for (Comment replay : replays) {
                //循环迭代，找出子代，存放到temReplys中
                recursively(replay);
            }
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setCommentList(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }

    }

    //存放迭代找出所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    private void recursively(Comment comment){
        tempReplys.add(comment); //顶节点添加到临时存放集合
        //为它的父亲赋值
        comment.setParentComment(commentMapper.selectByPrimaryKey(comment.getParentCommentId()));
        //这里为它的孩子赋值
        List<Comment> commentList = commentMapper.selectChildrens(comment.getId());
        if (commentList != null){
            comment.setCommentList(commentList);
        }
        if (comment.getCommentList().size() > 0 && comment.getCommentList() != null){
            List<Comment> replys = comment.getCommentList();
            for (Comment reply : replys) {
                System.out.println("我被调用了!!!");
                //为它的父亲赋值
                reply.setParentComment(commentMapper.selectByPrimaryKey(reply.getParentCommentId()));
                //这里为它的孩子赋值
                List<Comment> commentL = commentMapper.selectChildrens(reply.getId());
                if (commentL != null){
                    reply.setCommentList(commentL);
                }
                tempReplys.add(reply);
                if (reply.getCommentList().size()>0 && reply.getCommentList()!=null){
                    recursively(reply);
                }
            }
        }
    }

}
